﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YahooFinanceScraper
{
    public static class Utils
    {
        // May 1, 2015
        public static DateTime? ParseFullDate(string value)
        {
            DateTime? result = null;

            try
            {
                string[] arr = value.Split(',');

                int year = Convert.ToInt32(arr[1].Trim());
                arr = arr[0].Split(' ');
                int month = MonthFromString(arr[0]);
                int day = Convert.ToInt32(arr[1]);

                return new DateTime(year, month, day);
            }
            catch
            {
            }

            return result;
        }

        public static int MonthFromString(string value)
        {
            int result = 0;

            if (value == "January")
                result = 1;
            else if (value == "February")
                result = 2;
            else if (value == "March")
                result = 3;
            else if (value == "April")
                result = 4;
            else if (value == "May")
                result = 5;
            else if (value == "June")
                result = 6;
            else if (value == "July")
                result = 7;
            else if (value == "August")
                result = 8;
            else if (value == "September")
                result = 9;
            else if (value == "October")
                result = 10;
            else if (value == "November")
                result = 11;
            else if (value == "December")
                result = 12;

            return result;
        }

        public static void WriteLog(string message)
        {
            var pathTmp = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\Scarping.log";

            string log = "Дата: " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss");
            log += " " + message;

            System.IO.File.AppendAllText(pathTmp, log + Environment.NewLine);
        }

        public static string PrepareInnerText(string value)
        {
            return value.Replace(Environment.NewLine, "").Trim();
        }

    }
}
